package com.bosssoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.po.FibExpenseBudgetPO;
import com.bosssoft.entity.vo.FibExpenseBudgetVO;
import com.bosssoft.exception.DaoException;
import com.bosssoft.feign.DepartmentFeignService;
import com.bosssoft.mapper.FibExpenseBudgetMapper;
import com.bosssoft.model.SubjectExpenseModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.service.FibExpenseBudgetService;

import javax.annotation.Resource;

import static com.bosssoft.enums.BgtCode.CHANGE_STATUS_FAIL;

/**
 * @author Li kexi
 * Created on 2021/8/19.15:46
 */
@Service
@Slf4j
public class FibExpenseBudgetServiceImpl extends ServiceImpl<FibExpenseBudgetMapper, FibExpenseBudgetPO> implements FibExpenseBudgetService {

    @Resource
    private FibExpenseBudgetMapper fibExpenseBudgetMapper;

    @Autowired
    private DepartmentFeignService departmentFeignService;

    @Override
    public int updateBatch(List<FibExpenseBudgetPO> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<FibExpenseBudgetPO> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<FibExpenseBudgetPO> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public int insertOrUpdate(FibExpenseBudgetPO record) {
        return baseMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(FibExpenseBudgetPO record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    @Override
    public boolean selectById(Long eb_id, Integer status) {
        FibExpenseBudgetPO byId = getById(eb_id);
        byId.setStatus(status);
        boolean b = updateById(byId);
        return b;
    }

    @Override
    public Page<FibExpenseBudgetVO> selectByPage(Integer current, Integer size, String deptName, Integer year) {
        Page<FibExpenseBudgetPO> objectPage = new Page<>(current, size);
        LambdaQueryWrapper<FibExpenseBudgetPO> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(!StringUtils.isBlank(deptName), FibExpenseBudgetPO::getDeptName, deptName);
        queryWrapper.eq(year != 0, FibExpenseBudgetPO::getYear, year);

        Page<FibExpenseBudgetPO> page = page(objectPage, queryWrapper);
        Page<FibExpenseBudgetVO> page1 = new Page<>();
        BeanUtils.copyProperties(page,page1);
        return page1;
    }

    @Override
    public boolean updateByDetail(Long ebId, BigDecimal amount,Integer year) {

        FibExpenseBudgetPO fibExpenseBudgetPO = fibExpenseBudgetMapper.selectById(ebId);
        if (fibExpenseBudgetPO==null){
            FibExpenseBudgetPO fibExpenseBudgetPO1 = new FibExpenseBudgetPO();
            fibExpenseBudgetPO1.setEbId(ebId);

        }
        fibExpenseBudgetPO.setAmount(fibExpenseBudgetPO.getAmount().add(amount));
        fibExpenseBudgetPO.setYear(year);
        boolean b = save(fibExpenseBudgetPO);
        return b;
    }

    @Override
    public List<FibExpenseBudgetPO> getExpenseBudgetByDeptId(Long childrenId,Integer status) {

        List<FibExpenseBudgetPO> budgetPOS = fibExpenseBudgetMapper.getExpenseBudgetByDeptId(childrenId,status);
        return budgetPOS;
    }

    @Override
    public void changeStatus(FibExpenseBudgetPO expenseBudgetPO, Integer nextStatus) {
       try {
           expenseBudgetPO.setStatus(nextStatus);
           updateById(expenseBudgetPO);
       }catch (DaoException e){
           log.info("改变状态过程中出错");
           e.setCodeAndMessage(CHANGE_STATUS_FAIL);
       }
    }

    @Override
    public Map<Integer, BigDecimal> expenseSheet(Integer status,Integer year) {
        // 进库把所有的已经最后一级审核通过的（状态为4）的记录查出来 expense_budget
        List<SubjectExpenseModel> lists = fibExpenseBudgetMapper.selectByStatus(status,year);

        Map<Integer, BigDecimal> mapForEcoCode = new HashMap<>();
        // 遍历
        for (SubjectExpenseModel list:lists){
            mapForEcoCode.put(list.getEcoCode(),mapForEcoCode.get(list.getEcoCode()).add(list.getAmount()));
        }
        // 在遍历中判断支出类型（每一种相同类型的把金额加在一起）-> money
        // 返回Map<Integer,BigDecimal>（Integer是经济科目分类代码）
        return mapForEcoCode;
    }

    /**
     * 获取所有报表（按审核状态和年度）
     * @param deptId
     * @param year
     * @param status
     * @return
     */
    @Override
    public Map<Long, List<FibExpenseBudgetPO>> getAllStatement(Long deptId, Integer year, Integer status) {

        //当前单位所有子单位的id
        List<Long> childrenIds = departmentFeignService.selectChildrenIds(deptId);
        //用这个id去找到他们对应的支出预算表中的记录
        Map<Long, List<FibExpenseBudgetPO>> map = new HashMap<>();
        for (Long childrenId : childrenIds){
            List<FibExpenseBudgetPO> budgetPOS = getExpenseBudgetByDeptId(childrenId,status);
            map.put(childrenId,budgetPOS);
        }
        return map;
    }

    @Override
    public Map<Long, BigDecimal> expenseSheet1(Integer status, Integer year) {
        // 进库把所有的已经最后一级审核通过的（状态为4）的记录查出来 expense_budget
        List<SubjectExpenseModel> lists = fibExpenseBudgetMapper.selectByStatus(status,year);
        Map<Long, BigDecimal> mapForDeptId = new HashMap<>();
        // 遍历
        for (SubjectExpenseModel list:lists){
            mapForDeptId.put(list.getDeptId(),mapForDeptId.get(list.getDeptId()).add(list.getAmount()));
        }
        // 在遍历中判断支出类型（每一种相同类型的把金额加在一起）-> money
        // 返回Map<Integer,BigDecimal>（Integer是经济科目分类代码）
        return mapForDeptId;
    }
}

